//
//  SHPBottomToolBar.swift
//  weibo
//
//  Created by songhuipan on 16/4/23.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

import UIKit
enum bottomToolBarType:Int {
    /// 图片
    case Picture = 1001
    /// @
    case Mention = 1002
    /// #
    case Trend = 1003
    /// 表情
    case Emoticon = 1004
    /// +
    case Add = 1005
    
}

class SHPBottomToolBar: UIView {
    // 定义闭包，实现点击按钮回调
    var closeure:((type:bottomToolBarType)->())?
    
    var emticonButton:UIButton?
    
    var isEmoticon:Bool = false{
        didSet{
            if isEmoticon{
                emticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }else{
                emticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        self.backgroundColor = UIColor.redColor()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
extension SHPBottomToolBar {
    // MARK:设置视图UI
    func setupUI () {
        
        let pictureButton = addChildrenButton("compose_toolbar_picture", type: bottomToolBarType.Picture)
        let mentionButton = addChildrenButton("compose_mentionbutton_background", type: bottomToolBarType.Mention)
        let trendButton = addChildrenButton("compose_trendbutton_background", type: bottomToolBarType.Trend)
        emticonButton = addChildrenButton("compose_emoticonbutton_background", type: bottomToolBarType.Emoticon)
        let addButton = addChildrenButton("compose_add_background", type: bottomToolBarType.Add)
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(trendButton)
            make.leading.equalTo(pictureButton.snp_trailing)
        }
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emticonButton!)
        }
        emticonButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(addButton)
            make.leading.equalTo(trendButton.snp_trailing)
        }
        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            // MARK:这里必须设置前边约束，设置宽度会显示不正确
            make.leading.equalTo(emticonButton!.snp_trailing)
        }
    }
    // MARK:添加按钮的方法
    private func addChildrenButton (imageName:String,type:bottomToolBarType)->UIButton{
        
        let button = UIButton(type: UIButtonType.Custom)
        // 将枚举值转变成Int
        button.tag = type.rawValue
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
//        button.setBackgroundImage(UIImage(named: ""), forState: <#T##UIControlState#>)
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        addSubview(button)
        return button
    }
    // MARK:点击底部按钮调用的方法
    func buttonClick(button:UIButton) {
        
        if let buttonCloseure = closeure {
            
            buttonCloseure(type: bottomToolBarType(rawValue: button.tag)!)
            
        }
    }
}

































